package www.dianbaozu.com.ui.manage

import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.netease.nim.uikit.xpopup.CreateXPopupHelper
import com.netease.nim.uikit.xpopup.PopupViewEntity
import dagger.hilt.android.AndroidEntryPoint
import www.dianbaozu.com.R
import www.dianbaozu.com.databinding.FragmentEditMemberBinding
import www.dianbaozu.com.extension.requestUniversally
import www.wen.com.common.lib.base.fragment.BaseLoadingFragment
import www.wen.com.common.lib.base.module.BaseApiViewModel
import www.wen.com.common.lib.base.module.BaseViewModel
import www.wen.com.common.lib.data.entities.RequestBodyParameter
import www.wen.com.common.lib.data.entities.ServiceMemberEntity
import www.wen.com.common.lib.extension.*
import www.wen.com.common.lib.extension.glide.setImageUrl
import www.wen.com.common.lib.helper.loading.KeyHelper.BUNDLE_DATA
import www.wen.com.common.lib.helper.loading.KeyHelper.REFRESHING_SERVER_LIST2
import www.wen.com.common.lib.helper.loading.TitleBarUniversalAdapter

/**
 * @author WEN
 * @Description:编辑用户
 * @date 2020//25 17:48
 */
@AndroidEntryPoint
class EditMemberFragment : BaseLoadingFragment<FragmentEditMemberBinding>(), View.OnClickListener {

    private val mBaseApiViewModel: BaseApiViewModel by viewModels()

    private lateinit var mServiceMemberEntity: ServiceMemberEntity

    override val viewModel: BaseViewModel
        get() = mBaseApiViewModel

    override fun isEnableLoadingHelper(): Boolean = true

    override fun getTitleBarUniversalAdapter(): TitleBarUniversalAdapter = TitleBarUniversalAdapter(
            activity = requireActivity(),
            tilte = getString(R.string.com_edit_user),
            isShowLeftText = true,
            leftText = getString(R.string.com_close),
            action = ::titleAction
    )

    override fun getLayoutId(): Int = R.layout.fragment_edit_member

    override fun lazyInitView() {

        dataBinding.run {
            mServiceMemberEntity = requireArguments().getParcelable(BUNDLE_DATA)!!
            setImageUrl(ivAvatar,
                    mServiceMemberEntity.avatar.imgUrl(),
                    R.drawable.user_default_icon,
                    isCircle = true
            )
            tvKickOut.enableTextColor(mServiceMemberEntity.ownerFlag != "1")
            tvTransfer.enableTextColor(mServiceMemberEntity.ownerFlag != "1")
            tvNickname.text = mServiceMemberEntity.nickName
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            //踢人
            R.id.tvKickOut -> kickOut()
            //转让群主
            R.id.tvTransfer -> transfer()
        }
    }

    private fun transfer() {
        CreateXPopupHelper.createShowStandardAlertPopupView(PopupViewEntity().apply {
            context = requireActivity()
            standardAlertPopupViewTitle = getString(R.string.com_transfer_server_owner)
            isShow = true
            standardAlertPopupViewCancelContent = "取消"
            standardAlertPopupViewConfirmContent = "确认"
            standardAlertPopupViewContent = "确定要把服务器「${mServiceMemberEntity.serverName}」转让给成员 「${mServiceMemberEntity.nickName}」作为服务器主吗?\n\n该操作无法撤销"
            confirmFunction = {
                mBaseApiViewModel.requestUniversally(
                        viewModelScope = lifecycleScope,
                        tipSuccess = "转让服务器「${mServiceMemberEntity.nickName}」成功",
                        isShowToastTip = true,
                        request = { appDataManage ->
                            appDataManage.requestChangeOwnerOrKick(
                                    "change_owner",
                                    RequestBodyParameter(
                                            serverId = mServiceMemberEntity.serverId,
                                            userId = mServiceMemberEntity.userId,
                                    ))
                        }
                ) {
                    toast("转让服务器「${mServiceMemberEntity.nickName}」成功")
                    popProxy(R.id.mainFragment)
                }
            }
        })
    }

    private fun kickOut() {
        CreateXPopupHelper.createShowStandardAlertPopupView(PopupViewEntity().apply {
            context = requireActivity()
            standardAlertPopupViewTitle = getString(R.string.com_remove_members)
            isShow = true
            standardAlertPopupViewCancelContent = "取消"
            standardAlertPopupViewConfirmContent = "确认"
            standardAlertPopupViewContent = "确定要将 ${mServiceMemberEntity.nickName} 踢出服务器吗?\n\n该操作无法撤销"
            confirmFunction = {
                mBaseApiViewModel.requestUniversally(
                        viewModelScope = lifecycleScope,
                        tipSuccess = "",
                        isShowToastTip = true,
                        request = { appDataManage ->
                            appDataManage.requestChangeOwnerOrKick(
                                    "kick",
                                    RequestBodyParameter(
                                            serverId = mServiceMemberEntity.serverId,
                                            userId = mServiceMemberEntity.userId,
                                    ))
                        }
                ) {
                    toast("将 ${mServiceMemberEntity.nickName} 踢出服务器成功")
                    postObject(REFRESHING_SERVER_LIST2,mServiceMemberEntity)
                    actionLeft()
                }
            }
        })
    }

}



